package jp.co.sharp.android.decome.aobject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

public class AObject {
	// private int mDepth = 0; /* 視差情報 */
	// private float mDepthF = 1.0f; /* 3D飛び出し設定情報 */

	float x = 0;
	float y = 0;
	float z = 0;
	Bitmap bmp = null;

	public AObject() {
	}

	public float getX() {
		return this.x;
	}

	public float getY() {
		return this.y;
	}

	public float getZ() {
		return this.z;
	}

	public Bitmap getBmp() {
		return this.bmp;
	}

	public void setXYZ(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public void setBmp(Bitmap bmp) {
		this.bmp = bmp;
	}

	public void draw(Canvas canvas, float width, float height, float e) {
		if (bmp != null) {
			{
				float d = -e / (height / 10);
				d = z;
				float f = 1 / (2 - d);
				d += width / 2 + x / 2;
				Rect src = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
				RectF dst = new RectF(d - bmp.getWidth() * f / 2, height + y
						- bmp.getHeight() * f, d + bmp.getWidth() * f / 2,
						height + y + bmp.getHeight() * f);
				canvas.drawBitmap(bmp, src, dst, null);
			}
			{
				float d = e / (height / 10);
				d = z;
				float f = 1 / (2 - d);
				d += width * 3 / 2 + x / 2;
				Rect src = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
				RectF dst = new RectF(d - bmp.getWidth() * f / 2, height + y
						- bmp.getHeight() * f, d + bmp.getWidth() * f / 2,
						height + y + bmp.getHeight() * f);
				canvas.drawBitmap(bmp, src, dst, null);
			}
		} else {
			Paint paint = new Paint();
			paint.setColor(Color.BLUE);
			canvas.drawRect(width + x - e, height + y - e, width + x + e,
					height + y + e, paint);
		}
	}

	public static void sortByZ(ArrayList<AObject> list) {
		Collections.sort(list, new Comparator<AObject>() {
			public int compare(AObject object1, AObject object2) {
				return (int) Math.signum(object1.z - object2.z);
			}
		});
	}
}
